package com.wc.alorithm_luogu.P1434;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

/**
 * @Author congge
 * @Date 2023/9/28 19:29
 * @description 滑雪
 * https://www.luogu.com.cn/problem/P1434
 */
public class Main {
    public static void main(String[] args) {
        Input scan = new Input();

        int m = scan.nextInt();
        int n = scan.nextInt();
        int[][] ground = new int[m][n];

        /**
         * 记录所有的高度
         */
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                ground[i][j] = scan.nextInt();
            }
        }

        int[][] dp = new int[m][n];

        /**
         * 初始化每一个位置
         */
        for (int i = 0; i < m; i++) {
            Arrays.fill(dp[i], 1);
        }

        Main main = new Main();

        int maxLen = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                maxLen = Math.max(maxLen, main.deepScan(ground, i, j, dp));
            }
        }
        System.out.println(maxLen);
    }

    /**
     * 计算出当前位置的最大长度
     * @param ground 场地
     * @param i 场地横坐标
     * @param j 场地纵坐标
     * @param dp dp记忆
     * @return
     */
    public int deepScan(int[][] ground, int i, int j, int[][] dp) {
        /**
         * 如果当前位置不是 1 说明已经计算过了，就不用重复计算了
         */
        if (dp[i][j] != 1) {
            return dp[i][j];
        }

        int up = i - 1;
        int down = i + 1;
        int left = j - 1;
        int right = j + 1;

        int maxLen = 0;
        /**
         * 遍历上部分
         */
        if (isNotBorder(ground, up, j)) {
            /**
             * 比较高度问题
             */
            if (ground[up][j] > ground[i][j]) {
                maxLen = deepScan(ground, up, j, dp);
            }
        }

        /**
         * 遍历下部分
         */
        if (isNotBorder(ground, down, j)) {
            if (ground[down][j] > ground[i][j]) {
                maxLen = Math.max(maxLen, deepScan(ground, down, j, dp));
            }
        }

        /**
         * 遍历左部分
         */
        if (isNotBorder(ground, i, left)) {
            if (ground[i][left] > ground[i][j]) {
                maxLen = Math.max(maxLen, deepScan(ground, i, left, dp));
            }
        }

        /**
         * 遍历右部分
         */
        if (isNotBorder(ground, i, right)) {
            if (ground[i][right] > ground[i][j]) {
                maxLen = Math.max(maxLen, deepScan(ground, i, right, dp));
            }
        }

        dp[i][j] += maxLen;
        return dp[i][j];
    }

    /**
     * 检查是否不不是边界
     *
     * @param ground
     * @param i
     * @param j
     * @return
     */
    public boolean isNotBorder(int[][] ground, int i, int j) {
        return i >= 0 && j >= 0 && i != ground.length && j != ground[0].length;
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
